package com.novel.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.novel.common.pojo.RedisParamKey;
import com.novel.mapper.RechargeMapper;
import com.novel.pojo.system.Recharge;
import com.novel.utils.GsonTool;
import com.novel.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Slf4j
@Service
public class RechargeService {

    @Autowired
    private RechargeMapper rechargeMapper;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 查询所有充值类目
     */
    public List<Recharge> getRechargeList() {
        Example example = new Example(Recharge.class);
        example.setOrderByClause("cost");
        return rechargeMapper.selectByExample(example);
    }

    /**
     * 分页查询当前所有充值类目
     */
    public PageInfo<Recharge> getRechargeList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Recharge.class);
        example.setOrderByClause("cost desc");
        return new PageInfo<>(rechargeMapper.selectByExample(example));
    }

    /**
     * 增加充值类目
     */
    public boolean insertRechargeItem(Recharge recharge) {
        boolean result = rechargeMapper.insert(recharge) == 1;
        if (result) {
            redisUtils.stringSet(RedisParamKey.rechargeList,
                    GsonTool.toJson(getRechargeList()));
        }
        return result;
    }

    /**
     * 删除充值类目
     */
    public boolean deleteRechargeItemById(String id) {
        boolean result = rechargeMapper.deleteByPrimaryKey(id) == 1;
        if (result) {
            redisUtils.stringSet(RedisParamKey.rechargeList,
                    GsonTool.toJson(getRechargeList()));
        }
        return result;
    }

    /**
     * 更新充值类目
     */
    public boolean updateRechargeItem(Recharge recharge) {
        return rechargeMapper.updateByPrimaryKeySelective(recharge) == 1;
    }

}